package dapp

import (
	"github.com/spf13/cobra"
	"hundsun.com/hsl/hschain/cmd/cli/common"
	"hundsun.com/hsl/hschain/common/util"
	"hundsun.com/hsl/hschain/protos/config"
	service "hundsun.com/hsl/hschain/rpc/impl"
)

// ConsensusCmd consensus command
func ConsensusCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "consensus",
		Short: "consensus operation",
		Args:  cobra.MinimumNArgs(1),
	}

	cmd.AddCommand(
		queryConsensusCmd(),
	)
	return cmd
}

func queryConsensusCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "query",
		Short: "consensus query",
		Run:   queryConsensus,
	}
	return cmd
}

func queryConsensus(cmd *cobra.Command, args []string) {
	rpcLaddr, _ := cmd.Flags().GetString(common.FlagRPCLaddr)
	common.RPCCall(rpcLaddr, service.GetConsensusInfo, nil, func(res interface{}) {
		obj := res.(service.JSONRPCResp)
		val, _ := util.Bytes2Struct(&config.ConsensusParam{}, obj.Payload)
		common.PrintJSON(val)
	})
}
